package com.czxy.controller;

import com.czxy.rbac.SysUser;
import com.czxy.rbac.SysUserRole;
import com.czxy.service.SysUserRoleService;
import com.czxy.service.SysUserService;
import com.czxy.vo.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysUserRoleService userRoleService;

    @PostMapping("/add")
    public BaseResult addUser(@RequestBody SysUser user){
        boolean flag = userService.save(user);
        if (flag){
            //将用户和角色保存到用户橘色表中
            for(Integer roleId:user.getRoleIds()){
                SysUserRole userRole = new SysUserRole(user.getId().toString(), roleId);
                userRoleService.save(userRole);
            }
            return BaseResult.ok();
        }
        return BaseResult.error();
    }


}
